package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dk.data.R;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.TicatService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//分类管理表
@Controller
@RequestMapping("tcat")
public class TicatController {

    @DubboReference
    TicatService ts;


    //页面跳转
    @RequestMapping("tiao")
    public String tiao(){

        return "admin/item_cat";
    }



    //查询所有
    @RequestMapping("getall")
    @ResponseBody
    public R<List<TbItemCat>> getall(Long parentId){
        List<TbItemCat> ls = ts.getll(parentId);
        return R.ok().setData(ls);
    }


    //增加回显
    @RequestMapping("addteat")
    @ResponseBody
    public R<TbItemCat> addteat(Long id){
        TbItemCat tt = ts.addteat(id);
        return  R.ok().setData(tt);
    }

    //增加
    @RequestMapping("addbaocun")
    @ResponseBody
    public R addbaocun(TbItemCat tc){
        ts.addbaocun(tc);
        return R.ok();
    }


    //更新回显
    @RequestMapping("updatecat")
    @ResponseBody
    public R updatecat(Long id ,Long parentId,Model mo){
        TbItemCat tcat = ts.updatecat(id);
        TbItemCat t = ts.byid(parentId);
        mo.addAttribute("t",t);
        return R.ok().setData(tcat);
    }





    //更新
    @RequestMapping("updatebao")
    @ResponseBody
    public R updatebao(TbItemCat tc){
        ts.updatebao(tc);
        return R.ok();
    }


    //删除
    @RequestMapping("deletebyid")
    @ResponseBody
    public R deletebyid(Long id){
        ts.deletebyid(id);
        return R.ok();
    }


    //删除2删除父级
    @RequestMapping("deletearr")
    @ResponseBody
    public R deletearr(String arr){
        List<Long> parentid = JSONObject.parseArray(arr, Long.class);//将传过来的String类型的json转换为list集合
        ts.deletearr(parentid);
        return R.ok();
    }



}
